package com.baibei.ebec.user.systemmessage;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.ebec.sdk.TokenInfo;
import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.ebec.user.divider.DividerItemDecoration;
import com.baibei.model.MessageInfo;
import com.baibei.model.MsgContentInfo;
import com.baibei.model.MsgInfo;
import com.baibei.model.OrderInfo;
import com.baibei.model.SysMsgInfo;
import com.baibei.model.UserInfo;
import com.baibei.module.BasicFragment;
import com.baibei.module.LazyLoadFragment;
import com.baibei.module.LoadMoreAdapter;
import com.baibei.module.OnItemSelectListener;
import com.baibei.order.bill.SimpleRefreshListener;
import com.baibei.order.bill.sticky_header.StickyHeaderScrollListener;
import com.baibei.widget.CommonRefreshLayout;
import com.blankj.utilcode.utils.LogUtils;
import com.rae.swift.session.SessionManager;

import java.io.Console;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 */
public class SystemMessageFragment extends BasicFragment implements SystemMessageContract.SystemMessageView,OnItemSelectListener<SysMsgInfo> {

    private String accessToken;
    private String userId;
    private ArrayList<SysMsgInfo> sysMsgInfos;

    public SystemMessageFragment() {
    }
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_date)
    TextView mTvDate;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout mRefreshLayout;
    MessageAdapter mAdapter;
    LoadMoreAdapter mLoadMoreAdapter;

    private SystemMessageContract.Presenter mPresenter;

    public static SystemMessageFragment newInstance() {
        SystemMessageFragment fragment = new SystemMessageFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = inject(SystemMessageContract.Presenter.class);
        TokenInfo userToken = SessionManager.getDefault().getUserToken();
        UserInfo userInfo = SessionManager.getDefault().getUser();
        accessToken = userToken.getAccessToken();
        userId = userInfo.getUserId()+"";

        sysMsgInfos = new ArrayList<>();

        mPresenter.getMsgList(userId,accessToken);
    }


    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mAdapter = new MessageAdapter();
        mAdapter.setOnListener(this);
        
        mAdapter.setToptextGravity();
//        mLoadMoreAdapter = new LoadMoreAdapter(getContext(), mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//        mRecyclerView.setAdapter(mLoadMoreAdapter);
        mRecyclerView.setAdapter(mAdapter);
        //添加滚动监听
        mRecyclerView.addOnScrollListener(new StickyHeaderScrollListener(mTvDate));
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (mAdapter.getDataCount() != 0 && mTvDate.getVisibility() == View.GONE) {
                    mTvDate.setVisibility(View.VISIBLE);
                }
            }
        });

        mRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                mPresenter.getMsgList(userId,accessToken);
            }
        });
        mTvDate.setVisibility(View.GONE);
        mRefreshLayout.addPtrUIHandler(new SimpleRefreshListener() {
            @Override
            public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
                super.onUIRefreshPrepare(ptrFrameLayout);
                mTvDate.setVisibility(View.GONE);
            }
        });
//        List<MessageInfo> list=new ArrayList<>();
//        MessageInfo messageInfo1 = new MessageInfo(1, 1, "黄金已上涨到您设定的1000.00元, 请关注", "13:00","1530094141000");
//        MessageInfo messageInfo2 = new MessageInfo(1, 2, "您订购的转运珠—10件, 因达到差价上限，请密切关注最新动态.", "13:00","1530094141000");
//        MessageInfo messageInfo3 = new MessageInfo(1, 3, "您注册成功, 奖励一张价值8.8元的体验券", "13:00","1530094141000");
//        MessageInfo messageInfo4 = new MessageInfo(1, 3, "黄金已上涨到您设定的1000.00元, 请关注", "13:00","1530094141000");
//        MessageInfo messageInfo5 = new MessageInfo(1, 2, "黄金已上涨到您设定的1000.00元, 请关注", "13:00","1517292843000");
//        MessageInfo messageInfo6 = new MessageInfo(1, 1, "黄金已上涨到您设定的1000.00元, 请关注", "13:00","1517292843000");
//        list.add(messageInfo1);
//        list.add(messageInfo2);
//        list.add(messageInfo3);
//        list.add(messageInfo4);
//        list.add(messageInfo5);
//        list.add(messageInfo6);
//        mAdapter.setInfos(list);
//        mPresenter.getMsgList(userId,accessToken);
    }

    @Override
    public void onDestroy() {
        if (mPresenter!=null)
        {
            mPresenter.destroy();
        }
        super.onDestroy();
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_system_message;
    }


    @Override
    public void hideRefreshLayout() {
        if (mRefreshLayout != null && mRefreshLayout.isRefreshing()) {
            mRefreshLayout.refreshComplete();
        }
    }

    @Override
    public void onLoadMsg(List<MsgInfo> msgInfos) {
        sysMsgInfos.clear();
        for (int i = 0; i < msgInfos.size(); i++){
            List<MsgContentInfo> obj = msgInfos.get(i).getObj();
            for (int j = 0; j < obj.size(); j++) {
                sysMsgInfos.add(new SysMsgInfo(timeStrToSecond(msgInfos.get(i).getCreatedTime())+"",
                                                obj.get(j).getTitle(),
                                                obj.get(j).getMessage(),
                                                obj.get(j).getDtime(),
                                                obj.get(j).getState()));
            }
        }

        mAdapter.setInfos(sysMsgInfos);
        mAdapter.notifyDataSetChanged();
    }

    public Long timeStrToSecond(String time) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Long second = format.parse(time).getTime();
            return second;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1l;
    }

    @Override
    public void onItemSelectListener(View view, SysMsgInfo sysMsgInfo, int position) {

    }
}
